<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Session;
use DB;
use Crypt;

class LoginController extends Controller
{
    public function login()
    {
        return view('home.login.login');
    }
    //login
    public function dologin(Request $request)
    {
        $data = $request -> except('_token');
        // dd($data);
        //检测验证码是否正确
        $code = Session::get('milkcaptcha');

        if($code != $data['captcha'])
        {
            return back() -> with(['info' => '验证码错误']);
        }

        $res = DB::table('users') -> where('email', $data['email']) -> first();
        if(!$res)
        {
            return back() -> with(['info' => '您输入的邮箱不存在']);
        }
        $password = $res -> password;
        //解密
        $password = Crypt::decrypt($password);
        // dd($password);
        // 
        //判断密码
        if($data['password'] != $password)
        {
            return back() -> with(['info' => '您输入的邮箱或者密码错误']);
        }

        session(['master' => $res]);
        // dd(session('master'));
        return redirect('home') -> with(['info' => '登录成功']);
    }
    // 退出
    public function logout()
    {
        session() -> flush();
        // var_dump(session('master'));
        return redirect('/home/login') -> with(['info' => '退出成功']);
    }
}